// 使用 let 和 const 替换 var
const chessBoard = [];
let me = true;
let over = false;
const wins = [];
const myWin = [];
const computerWin = [];

// 使用箭头函数替换传统的函数声明
const drawLine = () => {
    for (let i = 0; i < 15; i++) {
        cnt.moveTo(15 + i * 30, 15);
        cnt.lineTo(15 + i * 30, 435);
        cnt.stroke();
        cnt.moveTo(15, 15 + i * 30);
        cnt.lineTo(435, 15 + i * 30);
        cnt.stroke();
    }
}

// 提取出来的点击事件处理函数
const handleBoardClick = (e) => {
    if (over) {
        return;
    }
    if (!me) {
        return;
    }
    let x = e.offsetX;
    let y = e.offsetY;
    let i = Math.floor(x / 30);
    let j = Math.floor(y / 30);
    if (chessBoard[i][j] == 0) {
        oneStep(i, j, me);
        chessBoard[i][j] = 1;
        for (let k = 0; k < count; k++) {
            if (wins[i][j][k]) {
                myWin[k]++;
                computerWin[k] = 6;
                if (myWin[k] == 5) {
                    alert("厉害，你赢了！！");
                    over = true;
                }
            }
        }
        if (!over) {
            me = !me;
            computerAI();
        }
    }
}

// 使用命名函数替换匿名函数
cnv.onclick = handleBoardClick;